function [residuals,moments] = objectFuncStep2Threshold_macro_spec(...
    theta2Values,selectTheta2,VarFactors,...
    Cov10Factors,Cov01Factors,factors,macros,econAct,thresholdLevel,switch_h0,switch_hx)

% We turn theta2Values into a struct 
for i=1:size(selectTheta2,1)
    name = selectTheta2(i,1);
    theta2.(name{1}) = theta2Values(i,1);
end

% Adding common elements across regimes
nx = length(switch_h0);
for i=1:nx
    if switch_h0(i,1) == 0
        name1 = ['h0',num2str(i),'_1'];
        name2 = ['h0',num2str(i),'_2'];
        theta2.(name2) = theta2.(name1);
    end
end
for i=1:nx
    for j=1:nx
        if switch_hx(i,j) == 0
            name1 = ['hx',num2str(i),num2str(j),'_1'];
            name2 = ['hx',num2str(i),num2str(j),'_2'];
            theta2.(name2) = theta2.(name1);
        end
    end
end

moments = Var1Threshold_GMM_moments_macro_spec(theta2,VarFactors,...
    Cov10Factors,Cov01Factors,factors,macros,econAct,thresholdLevel,switch_h0,switch_hx);

%Q = mean(moments,2)'*mean(moments,2);
residuals = mean(moments,2);

end